package com.xxx.grpc.client;

import com.xxx.grpc.protobuf.GreeterGrpc;
import com.xxx.grpc.protobuf.HelloReply;
import com.xxx.grpc.protobuf.HelloRequest;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

/**
 * gRPC client端
 */
public class HelloClient {

    public static void main(String[] args) {
        // 创建channel，连接到gRPC服务端
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50051)
                .usePlaintext()
                .build();
        // 创建Stub
        GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);

        // 调用gRPC接口1
        HelloRequest request = HelloRequest.newBuilder().setName("Alice").build();
        HelloReply reply = stub.sayHello(request);
        System.out.println("Response from server: " + reply.getMessage());

        // 调用gRPC接口2
        request = HelloRequest.newBuilder().setName("Bob").build();
        reply = stub.getAge(request);
        System.out.println("Response from server: " + reply.getMessage());

        // 关闭channel
        channel.shutdown();
    }
}